/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Xiaobao Zeng<zengxiaobao@skyworth.com>
 * Created Time: 2017-05-19 20:37:43
 * Last Modified: 2017-07-12 09:45:26
 * File Name: qkleague.go
 * Description:
 */

package qkleague

import (
	"crypto/tls"
	"encoding/json"
	"fmt"
	"time"

	"qkstatis/auth"

	"github.com/astaxie/beego/httplib"
	log "github.com/cihub/seelog"
)

//获取加盟商信息
func GetLeagueInfo(leagueId, sessionId string) (bool, map[string]interface{}) {
	curTick := time.Now().Unix()
	params := make(map[string]interface{})
	params["key"] = leagueId
	params["appkey"] = "af7d08b2f8407d907bfbf1a1c3d5dbfd"
	params["time"] = curTick
	params["type"] = 5
	params["sessionid"] = sessionId
	sign := auth.GetURLSignService().CalcURLSignByMap(params, "dfa8c712433b83b8d827d9386fa2fa46")
	reqURL := fmt.Sprintf("https://%s/admin/league/some?appkey=af7d08b2f8407d907bfbf1a1c3d5dbfd&type=%d&key=%s&time=%d&sessionid=%s&sign=%s", qklHost, 5, leagueId, curTick, sessionId, sign)
	b := httplib.Get(reqURL)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-GetLeagueInfo, service page not found, url:%d", reqURL)
		return false, nil
	}
	if int(resp["code"].(float64)) == 0 {
		b, err := json.Marshal(resp["data"])
		if err != nil {
			log.Errorf("json err, err:%s", err.Error())
		}
		data := make([]interface{}, 0)
		if err := json.Unmarshal(b, &data); err != nil {
			log.Errorf("json err, err:%s", err.Error())
		} else {
			return true, data[0].(map[string]interface{})
		}
	}
	log.Warnf("rpc-GetLeagueInfo,resp err, code:%v, msg:%v\n", resp["code"], resp["msg"])
	return false, nil
}

//获取洗衣点信息
func GetWashRoomInfo(washRoomId, sessionId string) (bool, map[string]interface{}) {
	curTick := time.Now().Unix()
	params := make(map[string]interface{})
	params["appkey"] = "af7d08b2f8407d907bfbf1a1c3d5dbfd"
	params["time"] = curTick
	params["washroom_id"] = washRoomId
	params["sessionid"] = sessionId
	sign := auth.GetURLSignService().CalcURLSignByMap(params, "dfa8c712433b83b8d827d9386fa2fa46")
	reqURL := fmt.Sprintf("https://%s/admin/washroom/some?appkey=af7d08b2f8407d907bfbf1a1c3d5dbfd&washroom_id=%s&time=%d&sessionid=%s&sign=%s", qklHost, washRoomId, curTick, sessionId, sign)
	b := httplib.Get(reqURL)
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	resp := make(map[string]interface{})
	if err := b.ToJSON(&resp); err != nil {
		log.Errorf("rpc-GetWashRoomInfo, service page not found, url:%d", reqURL)
		return false, nil
	}
	if int(resp["code"].(float64)) == 0 {
		log.Debugf("%T, %v\n", resp["data"], resp["data"])
		b, err := json.Marshal(resp["data"])
		if err != nil {
			log.Errorf("json err, err:%s", err.Error())
		}
		data := make([]interface{}, 0)
		if err := json.Unmarshal(b, &data); err != nil {
			log.Errorf("json err, err:%s", err.Error())
		} else {
			return true, data[0].(map[string]interface{})
		}
	}
	log.Warnf("rpc-GetWashRoomInfo,resp err, code:%v, msg:%v", resp["code"], resp["msg"])
	return false, nil
}
